package org.utahgtug.site.view;

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.json.JsonHttpRequest;
import com.google.api.client.http.json.JsonHttpRequestInitializer;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.calendar.Calendar;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Controller;

import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: gmdayley
 * Date: 1/4/12
 * Time: 3:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class CalendarServiceFactoryBean implements FactoryBean<Calendar> {
    private static final String CALENDAR_API_KEY = "AIzaSyBV9FOspVebohTR_zZgC3Jk0EQW32z8eVw"; //key=yourAPIKey

    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    @Override
    public Calendar getObject() throws Exception {
        return Calendar.builder(httpTransport, jsonFactory)
                .setApplicationName("ugtug")
                .setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
                    @Override
                    public void initialize(JsonHttpRequest request) throws IOException {
                        request.set("key", CALENDAR_API_KEY);
                    }
                })
                .build();
    }

    @Override
    public Class<Calendar> getObjectType() {
        return Calendar.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
